package com.example.common;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;


@Data
@AllArgsConstructor
public class InvokeResult<T> implements Serializable {
    private String status;
    private String message;
    private T data;

    private final static String STATUS_SUCCESS = "0";
    private final static String MESSAGE_SUCCESS = "SUCCESS";

    public InvokeResult(T data) {
        this.status = STATUS_SUCCESS;
        this.message = MESSAGE_SUCCESS;
        this.data = data;
    }

    public InvokeResult(String status, String message) {
        this.status = status;
        this.message = message;
    }

    public InvokeResult() {
        this(null);
    }

    public static <T> InvokeResult<T> success() {
        return new InvokeResult<>();
    }

    public static <T> InvokeResult<T> success(String status, String message){
        return new InvokeResult(status, message);
    }

    public static <T> InvokeResult<T> success(T data) {
        return new InvokeResult<>(data);
    }
    public static <T> InvokeResult<T> error(String message) {
        return new InvokeResult("-1",message);
    }

    public static InvokeResult<StackTraceElement> error(Exception exception) {
        return new InvokeResult<>("-1", exception.getMessage(), exception.getCause() == null ? exception.getStackTrace()[0] : exception.getCause().getStackTrace()[0]);
    }

    public static InvokeResult<StackTraceElement> runTimeError(Exception exception) {
        return new InvokeResult<>("-1", exception.getMessage());
    }

}
